<template>
  <div class="app-container">
    <el-form :model="query.key" ref="queryForm" size="small" :inline="true">
<!--      <el-form-item label="菜单名称" prop="menuName">-->
<!--        <el-input-->
<!--          v-model="query.key.processName"-->
<!--          placeholder="请输入菜单名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="query.key.processStatus" placeholder="菜单状态" clearable>
          <el-option
            v-for="dict in processStatusList"
            :key="dict.index"
            :label="dict.name"
            :value="dict.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item>

      </el-form-item>
    </el-form>

    <div>
      <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="handleMultipartDelete()">删除</el-button>
    </div>

    <el-table
      ref="multipleTable"
      :data="list"
      border
      tooltip-effect="dark"
      style="width: 100%; margin-top: 20px;"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        type="index"
        label="序号"
        width="55"
        align="center"
        sortable
        :index="indexMethod">
      </el-table-column>
      <el-table-column
        prop="processName"
        align="center"
        label="流程模板名称">
      </el-table-column>
      <el-table-column
        prop="processTitle"
        align="center"
        label="流程标题">
      </el-table-column>
      <el-table-column
        prop="processStatus"
        align="center"
        label="流程状态"
        width="160">
      </el-table-column>
      <el-table-column
        prop="startUserName"
        align="center"
        label="发起人">
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleOpenProcess(scope.row.processInstanceId, scope.row.processTitle)">打开</el-button>
          <el-dropdown>
            <el-button type="text" size="small">更多<i class="el-icon-caret-bottom"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleOpenProcessVar(scope.row.processInstanceId)">流程变量</el-dropdown-item>
              <el-dropdown-item @click.native="handleOpenProcessFollow(scope.row.processInstanceId)">流程跟踪</el-dropdown-item>
              <el-dropdown-item >挂起</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row.processInstanceId)">删除</el-dropdown-item>
<!--              <el-dropdown-item @click.native="handleDelete(scope.row.deploymentId)">删除表单</el-dropdown-item>-->
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page + 1"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="query.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" style="float: right; margin-top: 20px;">
    </el-pagination>

    <ProcessControl ref="processControl"></ProcessControl>

    <ProcessAttr ref="processAttr"></ProcessAttr>

    <ProcessFollow ref="processFollow"></ProcessFollow>

    <el-dialog
      :visible.sync="openProcessVar"
      :fullscreen="true"
      :destroy-on-close="true"
      :show-close="false">
      <div style="
      border-bottom: 1px solid rgb(232,232,232);
      position: absolute;
      top: 0; left: 0;
      height: 70px; width: 100%;
      line-height: 70px; padding-left: 20px; padding-right: 20px;">
        <span style="font-weight: bold; font-size: 20px;">流程变量</span>

        <div style="float:right;">
          <el-button plain icon="el-icon-close" @click="openProcessVar = false"></el-button>
        </div>
      </div>

      <el-card class="box-card" style="margin: 40px;">
        <div slot="header" class="clearfix">
          <span style="font-weight: bold; font-size: 20px;">审批意见</span>
        </div>
        <div class="text item">
          <el-table
            ref="multipleTable"
            :data="processVarList"
            border
            tooltip-effect="dark"
            style="width: 100%; margin-top: 20px;">
            <el-table-column
              type="index"
              label="序号"
              width="55"
              align="center"
              sortable>
            </el-table-column>
            <el-table-column
              prop="name"
              align="center"
              label="变量名">
            </el-table-column>
            <el-table-column
              prop="user"
              align="center"
              label="变量值">
            </el-table-column>
            <el-table-column
              prop="processStatus"
              align="center"
              label="变量类型">
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="endTime" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.endTime) }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import {getProcessControl, deleteProcessControl} from '@/api/camunda/camunda'

import ProcessControl from '@/components/Camunda/processControl'
import ProcessAttr from '@/components/Camunda/processAttr'
import ProcessFollow from '@/components/Camunda/processFollow'

export default {
  name: "index",
  components: {
    ProcessControl,
    ProcessAttr,
    ProcessFollow,
  },
  data() {
    return {
      list: [],
      multiple: [],
      query: {
        page: 0,
        size: 10,
        key: {
          processName: '',
          processStatus: ''
        }
      },
      total: 0,
      processStatusList: [
        {index: 1, name: "COMPLETED"},
        {index: 2, name: "ACTIVE"},
        {index: 3, name: "SUSPENDED"}
      ],
      title: '流程',
      repName: 0,
      openProcess: false,
      processInstanceId: undefined,
      openProcessVar: false,
      processVarList: [],
    }
  },
  created() {
    this.init()
  },
  methods: {
    init(){
      // 初始化数据模型列表
      getProcessControl(this.query)
        .then(({data}) => {
          this.total = data.total
          this.list = data.rows
        }).catch()
    },
    handleQuery() {
      this.init()
    },
    resetQuery() {
      this.query.key = {
        processName: '',
        processStatus: ''
      }
      this.init()
    },
    indexMethod(index) {
      const sum = this.query.size * (this.query.page)
      return index + sum + 1
    },
    handleCurrentChange(val) {
      this.query.page = val - 1
      this.init()
    },
    handleSizeChange(val) {
      this.query.size = val
      this.init()
    },
    handleSelectionChange(val) {
      this.multiple = val;
    },
    handleCancel() {
      this.reset()
      this.open = false
    },
    reset() {
      this.form = {
        id: undefined,
        name: '',
        describe: '',
        status: '1',
        sort: 0,
        createTime: null,
        updateTime: null
      }
    },
    handleMultipartDelete() {
      const ids = []
      this.multiple.forEach((item) => {
        ids.push(item.processInstanceId)
      })

      this.$confirm('共选中'+ ids.length +'项，此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProcessControl(ids)
          .then(() => {
            this.$message({type: 'success', message: '删除成功!'})
            this.init();
          }).catch(() => {this.$message({type: 'error', message: '删除失败!'})})
      }).catch()
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProcessControl(id)
          .then(() => {
            this.$message({type: 'success', message: '删除成功!'})
            this.init();
          }).catch(() => {this.$message({type: 'error', message: '删除失败!'})})
      }).catch(() => {})
    },
    handleOpenProcess(processInstanceId, processTitle) {
      this.$refs.processControl.init(processInstanceId, processTitle)
    },
    handleOpenProcessVar(processInstanceId) {
      this.$refs.processAttr.init(processInstanceId)
    },
    handleOpenProcessFollow(processInstanceId) {
      this.$refs.processFollow.init(processInstanceId)
    },
  }
}
</script>

<style>
.highlight .djs-visual > :nth-child(1) {
  stroke: green !important;
  text-color: green !important;
  /*fill: rgba(0, 80, 0, 0.4) !important;*/
}
.highlightIDO .djs-visual > :nth-child(1) {
  stroke: rgb(255, 196, 0) !important;
  /*fill: rgba(255, 196, 0, 0.4) !important;*/
}
.highlightTODO .djs-visual > :nth-child(1) {
  stroke: rgb(255, 0, 0) !important;
  /*fill: rgba(255, 255, 255, 0.4) !important;*/
}
</style>

<style scoped>

</style>
